#!/bin/bash

cat >/etc/init/rc-sysinit.override <<"EOF"
start on umm
EOF

cat >/etc/init/failsafe.override <<"EOF"
stop on static-network-up or starting rc-sysinit or starting umm
EOF

cat >/etc/init/umm.conf <<"EOF"
# umm - Openstak maiteenace mode.
#

description "Openstack maintenance mode"
author      "pzhurba@mirantis.com"

start on (filesystem and static-network-up) or failsafe-boot

emits umm

script
[ -f /usr/lib/umm/umm_svc ] && /usr/lib/umm/umm_svc
initctl emit umm
end script
EOF

cat >/etc/grub.d/55_umm << "EOF"
if [ -f /etc/grub.d/10_linux ] ; then
    /etc/grub.d/10_linux | sed -n -e '/menuentry.*{/,/}/p' |  sed -e "s/menuentry\s*'[^']*' /menuentry 'Unified maintenance mode (UMM)' /" -e 's/\(^\s*linux\s.*\)/\1 umm/'  -e '/}/,$d'; echo '}'
fi
EOF

chmod +x /etc/grub.d/55_umm

update-grub